import mongoose from 'mongoose';

const questionSchema = new mongoose.Schema({
  question: {
    type: String,
    required: true
  },
  options: {
    type: [String],
    required: true,
    validate: [arr => arr.length === 4, '选项必须有4个']
  },
  correctAnswer: {
    type: Number,
    required: true,
    min: 0,
    max: 3
  },
  explanation: {
    type: String,
    required: true
  },
  caseType: {
    type: String,
    required: true,
    enum: ['人格权纠纷', '婚姻家庭与继承纠纷', '物权纠纷', '合同与准合同纠纷', 
           '侵权责任纠纷', '知识产权纠纷', '劳动争议', '商事纠纷'],
    index: true
  }
}, { _id: false });

const questionBankSchema = new mongoose.Schema({
  caseTypes: {
    type: [String],
    required: true,
    index: true
  },
  questions: {
    type: [questionSchema],
    required: true,
    validate: [arr => arr.length > 0, '题库不能为空']
  },
  createdAt: {
    type: Date,
    default: Date.now,
    expires: '7d' // 7天后自动过期
  }
});

// 创建复合索引
questionBankSchema.index({ caseTypes: 1 });
questionBankSchema.index({ 'questions.caseType': 1 }); // 添加案例类型索引

const QuestionBank = mongoose.model('QuestionBank', questionBankSchema);
export default QuestionBank;
